package com.moleq.mg_pos_registration;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import com.example.mg_pos_registration.R;
import com.moleq.util.OutTxt;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.support.v4.app.NavUtils;
import android.text.Editable;

public class LoginActivity extends Activity
{
	private EditText userId = null;
	private EditText passWord = null;
	private Button login = null;
	private Button cancel = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		userId = (EditText) findViewById(R.id.editText1);
		passWord = (EditText) findViewById(R.id.editText2);
		login = (Button) findViewById(R.id.button1);
		cancel = (Button) findViewById(R.id.button2);
		
		login.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				String userIdString = userId.getText().toString().trim();
				String passWordString = passWord.getText().toString().trim();
				
				int flag = getLoginFlag(userIdString, passWordString);
				if (flag == 1)
				{
					System.out.println("1");
				}
				else 
				{
					System.out.println("0");
				}
			}
		});
		
		cancel.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				
				
			}
		});
	}
	
	public int getLoginFlag(String v_userId, String v_passWord)
	{
		String nameSpace = "http://tempuri.org/";
		String methodName = "Login";
		String endPoint = "http://10.0.2.2:1767/Service1.asmx";
		String soapAction = "http://tempuri.org/Login";
		
		SoapObject rpc = new SoapObject(nameSpace, methodName);
		rpc.addProperty("userID", v_userId);
		rpc.addProperty("pwd", v_passWord);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER10);

		envelope.bodyOut = rpc;
		envelope.dotNet = true;

		envelope.setOutputSoapObject(rpc);
		HttpTransportSE transport = new HttpTransportSE(endPoint);
		transport.debug = true;

		try
		{
			transport.call(soapAction, envelope);
		} catch (XmlPullParserException ex)
		{
			ex.printStackTrace();
		}

		catch (Exception e)
		{
			e.printStackTrace();

		}

		SoapObject object = (SoapObject) envelope.bodyIn;
		String result = object.getProperty(0).toString();
		return Integer.parseInt(result);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.activity_login, menu);
		return true;
	}

}
